package com.shiningstars.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

import com.shiningstars.R;
import com.shiningstars.general.Config;
import com.shiningstars.general.Constants;
import com.shiningstars.general.Messages;
import com.shiningstars.general.Utilities;
import com.shiningstars.tools.ExceptionHandler;
import com.shiningstars.valuebeans.Feedback;

public class ViewFeedback3Activity extends Activity implements Constants, Messages, Config {

	Feedback feedback;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_teacher_feedback_3);

			Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
			SplashPageActivity.showSplash = false;

			getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

			RadioButton radioMainTopicShort = (RadioButton) findViewById(R.id.tf3RadioButton1);
			RadioButton radioMainTopicProper = (RadioButton) findViewById(R.id.tf3RadioButton2);
			RadioButton radioMainTopicLengthy = (RadioButton) findViewById(R.id.tf3RadioButton3);
			RatingBar ratingBarMainTopicResponse = (RatingBar) findViewById(R.id.tf3RatingBar1);
			RadioButton radioMainTopicPptYes = (RadioButton) findViewById(R.id.tf3RadioButton4);
			RadioButton radioMainTopicPptNo = (RadioButton) findViewById(R.id.tf3RadioButton5);
			EditText editTextMainTopicComments = (EditText) findViewById(R.id.tf3EditText1);

			RadioButton radioSutraShort = (RadioButton) findViewById(R.id.tf3RadioButton6);
			RadioButton radioSutraProper = (RadioButton) findViewById(R.id.tf3RadioButton7);
			RadioButton radioSutraLengthy = (RadioButton) findViewById(R.id.tf3RadioButton8);
			RatingBar ratingBarSutraResponse = (RatingBar) findViewById(R.id.tf3RatingBar2);
			RadioButton radioSutraPptYes = (RadioButton) findViewById(R.id.tf3RadioButton9);
			RadioButton radioSutraPptNo = (RadioButton) findViewById(R.id.tf3RadioButton10);
			EditText editTextSutraComments = (EditText) findViewById(R.id.tf3EditText2);

			RadioButton radioStoryShort = (RadioButton) findViewById(R.id.tf3RadioButton11);
			RadioButton radioStoryProper = (RadioButton) findViewById(R.id.tf3RadioButton12);
			RadioButton radioStoryLengthy = (RadioButton) findViewById(R.id.tf3RadioButton13);
			RatingBar ratingBarStoryResponse = (RatingBar) findViewById(R.id.tf3RatingBar3);
			RadioButton radioStoryPptYes = (RadioButton) findViewById(R.id.tf3RadioButton14);
			RadioButton radioStoryPptNo = (RadioButton) findViewById(R.id.tf3RadioButton15);
			EditText editTextStoryComments = (EditText) findViewById(R.id.tf3EditText3);

			RadioButton radioValueShort = (RadioButton) findViewById(R.id.tf3RadioButton16);
			RadioButton radioValueProper = (RadioButton) findViewById(R.id.tf3RadioButton17);
			RadioButton radioValueLengthy = (RadioButton) findViewById(R.id.tf3RadioButton18);
			RatingBar ratingBarValueResponse = (RatingBar) findViewById(R.id.tf3RatingBar4);
			RadioButton radioValuePptYes = (RadioButton) findViewById(R.id.tf3RadioButton19);
			RadioButton radioValuePptNo = (RadioButton) findViewById(R.id.tf3RadioButton20);
			EditText editTextValueComments = (EditText) findViewById(R.id.tf3EditText4);

			ratingBarMainTopicResponse.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

				@Override
				public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
					TextView textViewMainTopicResponse = (TextView) findViewById(R.id.tf3TextView1);
					Utilities.setRatingDescription(textViewMainTopicResponse, rating);
				}
			});

			ratingBarSutraResponse.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

				@Override
				public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
					TextView textViewSutraResponse = (TextView) findViewById(R.id.tf3TextView2);
					Utilities.setRatingDescription(textViewSutraResponse, rating);
				}
			});

			ratingBarStoryResponse.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

				@Override
				public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
					TextView textViewStoryResponse = (TextView) findViewById(R.id.tf3TextView3);
					Utilities.setRatingDescription(textViewStoryResponse, rating);
				}
			});

			ratingBarValueResponse.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

				@Override
				public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
					TextView textViewValueResponse = (TextView) findViewById(R.id.tf3TextView4);
					Utilities.setRatingDescription(textViewValueResponse, rating);
				}
			});

			if (null != getIntent()) {
				if (null != getIntent().getExtras()) {
					if (null != getIntent().getExtras().get(FEEDBACK)) {
						feedback = (Feedback) getIntent().getExtras().get(FEEDBACK);

						if (SHORT.equals(feedback.getQuestion21())) {
							radioMainTopicShort.setChecked(true);
						} else if (PROPER.equals(feedback.getQuestion21())) {
							radioMainTopicProper.setChecked(true);
						} else if (LENGTHY.equals(feedback.getQuestion21())) {
							radioMainTopicLengthy.setChecked(true);
						}

						if (!Utilities.isBlankString(feedback.getQuestion22())) {
							float rating = Float.parseFloat(feedback.getQuestion22());
							ratingBarMainTopicResponse.setRating(rating);
						}

						if (YES.equals(feedback.getQuestion23())) {
							radioMainTopicPptYes.setChecked(true);
						} else if (NO.equals(feedback.getQuestion23())) {
							radioMainTopicPptNo.setChecked(true);
						}

						editTextMainTopicComments.setText(feedback.getQuestion24());

						if (SHORT.equals(feedback.getQuestion25())) {
							radioSutraShort.setChecked(true);
						} else if (PROPER.equals(feedback.getQuestion25())) {
							radioSutraProper.setChecked(true);
						} else if (LENGTHY.equals(feedback.getQuestion25())) {
							radioSutraLengthy.setChecked(true);
						}

						if (!Utilities.isBlankString(feedback.getQuestion26())) {
							float rating = Float.parseFloat(feedback.getQuestion26());
							ratingBarSutraResponse.setRating(rating);
						}

						if (YES.equals(feedback.getQuestion27())) {
							radioSutraPptYes.setChecked(true);
						} else if (NO.equals(feedback.getQuestion27())) {
							radioSutraPptNo.setChecked(true);
						}

						editTextSutraComments.setText(feedback.getQuestion28());

						if (SHORT.equals(feedback.getQuestion29())) {
							radioStoryShort.setChecked(true);
						} else if (PROPER.equals(feedback.getQuestion29())) {
							radioStoryProper.setChecked(true);
						} else if (LENGTHY.equals(feedback.getQuestion29())) {
							radioStoryLengthy.setChecked(true);
						}

						if (!Utilities.isBlankString(feedback.getQuestion30())) {
							float rating = Float.parseFloat(feedback.getQuestion30());
							ratingBarStoryResponse.setRating(rating);
						}

						if (YES.equals(feedback.getQuestion31())) {
							radioStoryPptYes.setChecked(true);
						} else if (NO.equals(feedback.getQuestion31())) {
							radioStoryPptNo.setChecked(true);
						}

						editTextStoryComments.setText(feedback.getQuestion32());

						if (SHORT.equals(feedback.getQuestion33())) {
							radioValueShort.setChecked(true);
						} else if (PROPER.equals(feedback.getQuestion33())) {
							radioValueProper.setChecked(true);
						} else if (LENGTHY.equals(feedback.getQuestion33())) {
							radioValueLengthy.setChecked(true);
						}

						if (!Utilities.isBlankString(feedback.getQuestion34())) {
							float rating = Float.parseFloat(feedback.getQuestion34());
							ratingBarValueResponse.setRating(rating);
						}

						if (YES.equals(feedback.getQuestion35())) {
							radioValuePptYes.setChecked(true);
						} else if (NO.equals(feedback.getQuestion35())) {
							radioValuePptNo.setChecked(true);
						}

						editTextValueComments.setText(feedback.getQuestion36());

						radioMainTopicShort.setEnabled(false);
						radioMainTopicProper.setEnabled(false);
						radioMainTopicLengthy.setEnabled(false);
						ratingBarMainTopicResponse.setEnabled(false);
						radioMainTopicPptYes.setEnabled(false);
						radioMainTopicPptNo.setEnabled(false);
						editTextMainTopicComments.setEnabled(false);
						radioSutraShort.setEnabled(false);
						radioSutraProper.setEnabled(false);
						radioSutraLengthy.setEnabled(false);
						ratingBarSutraResponse.setEnabled(false);
						radioSutraPptYes.setEnabled(false);
						radioSutraPptNo.setEnabled(false);
						editTextSutraComments.setEnabled(false);
						radioStoryShort.setEnabled(false);
						radioStoryProper.setEnabled(false);
						radioStoryLengthy.setEnabled(false);
						ratingBarStoryResponse.setEnabled(false);
						radioStoryPptYes.setEnabled(false);
						radioStoryPptNo.setEnabled(false);
						editTextStoryComments.setEnabled(false);
						radioValueShort.setEnabled(false);
						radioValueProper.setEnabled(false);
						radioValueLengthy.setEnabled(false);
						ratingBarValueResponse.setEnabled(false);
						radioValuePptYes.setEnabled(false);
						radioValuePptNo.setEnabled(false);
						editTextValueComments.setEnabled(false);
					}
				}
			}
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), ViewFeedback3Activity.class.getName(), e);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		try {
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.feedback_menu, menu);
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), ViewFeedback3Activity.class.getName(), e);
		}

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		try {
			Intent i = null;

			if (BACK.equals(item.getTitle())) {
				i = new Intent(ViewFeedback3Activity.this, ViewFeedback2Activity.class);
			} else if (NEXT.equals(item.getTitle())) {
				i = new Intent(ViewFeedback3Activity.this, ViewFeedback4Activity.class);
			}
			i.putExtra(FEEDBACK, feedback);
			startActivity(i);
			finish();
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), ViewFeedback3Activity.class.getName(), e);
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		try {
			AlertDialog.Builder builder = new AlertDialog.Builder(ViewFeedback3Activity.this);

			builder.setMessage(FINISHED_VIEW_FEEDBACK);
			builder.setCancelable(false);
			builder.setPositiveButton(OK, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					Intent i = null;

					String currentRole = Utilities.getSharedPreference(getApplicationContext(), CURRENT_ROLE);
					if (ADMIN.equals(currentRole)) {
						i = new Intent(ViewFeedback3Activity.this, AdminActivity.class);
					} else if (TEACHER.equals(currentRole)) {
						i = new Intent(ViewFeedback3Activity.this, MyFeedbackActivity.class);
					}

					startActivity(i);
					finish();
				}
			});
			builder.setNegativeButton(CANCEL, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {

				}
			});

			AlertDialog alert = builder.create();
			alert.show();
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), ViewFeedback3Activity.class.getName(), e);
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		SplashPageActivity.showSplash = true;
	}

	@Override
	protected void onResume() {
		super.onResume();

		if (SplashPageActivity.showSplash) {
			LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
			View popupView = layoutInflater.inflate(R.layout.activity_splash_page, null);
			final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT, true);

			findViewById(R.id.tf3TextView1).post(new Runnable() {
				public void run() {
					popupWindow.showAtLocation(findViewById(R.id.tf3TextView1), Gravity.CENTER, 0, 0);
				}
			});

			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					if (null != popupWindow && popupWindow.isShowing()) {
						popupWindow.dismiss();
					}
				}
			}, SPLASH_TIME_OUT);

			SplashPageActivity.showSplash = false;
		}
	}
}